package de.mk.swing;

import java.awt.Image;
import java.awt.Toolkit;
import java.net.URL;
import java.util.Properties;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

import de.mk.model.Board;
import de.mk.model.Picture;
import de.mk.model.Picture.State;


public class BoardPanel extends JPanel {

//	private final static Logger log = Logger.getLogger(FileUtil.class);
	
	private Board board = null;
	private PictureLabel[] pictureLabel = null;

	public BoardPanel(Board board, Properties props) {
		this.board = board;
		initPictureLabels(props);
	}

	private void initPictureLabels(Properties props) {

		int numOfCards = board.getNumOfCards();
		pictureLabel = new PictureLabel[numOfCards];

		for (int i = 0; i < numOfCards/ 2; i++) {

			URL url = getClass().getResource(
					props.getProperty("image_path") + (i + 1) + ".jpg");			
			Image img = Toolkit.getDefaultToolkit().getImage(url);
			ImageIcon imgIcon = new ImageIcon(img);

			Picture pic1 = board.getPicture(i);
			pictureLabel[i] = new PictureLabel(pic1, imgIcon);
			pictureLabel[i].setImage(img);
			
			Picture pic2 = board.getPicture(i);
			int j = i + (numOfCards / 2);			
			pictureLabel[j] = new PictureLabel(pic2, imgIcon);
			pictureLabel[j].setImage(img);
				
		}

	}

	@Override
	public void paintComponent(java.awt.Graphics g) {
		for (int i = 0; i < pictureLabel.length; i++) {
			int position = board.getPicture(i).getPosition();
			
			switch(board.getPicture(position).getState()){
			case uncovered:
				this.add(pictureLabel[position]);
				pictureLabel[position].setVisible(true);
//			case hidden:
//				this.add(pictureLabel[position]);
//				pictureLabel[position].setVisible(true);
//			case unvisible:
//				this.add(pictureLabel[position]);
//				pictureLabel[position].setVisible(false	);	
			}					
		}
	}

	public Board getBoard() {
		return board;
	}

	public void setBoard(Board board) {
		this.board = board;
	}

	public PictureLabel[] getPictureLabel() {
		return pictureLabel;
	}

	public void setPictureLabel(PictureLabel[] pictureLabel) {
		this.pictureLabel = pictureLabel;
	}

}
